/*
 * UDP Server
 * Project 3 
 * Created by: Wilson Burhan & Derrick Mink
 */

import java.io.*;
import java.net.*;
import java.util.Scanner;

/**
 * The UDP echo server program to communicates with the echo client program.
 * @author Wilson Burhan & Derrick Mink
 */
public class UDPServer {
	public static void main(String[] args) throws IOException {
		//This part of the code is to specify the port number 
	    DatagramSocket serverSocket = null;
	    MulticastSocket mSocket = new MulticastSocket();
	    System.out.print("Please enter the port number: ");
	    Scanner scan = new Scanner(System.in);
	    int port = scan.nextInt();
	    System.out.print("Please enter the multicast server address: ");
        String mHost = scan.next();
	    System.out.print("Please enter the multicast port: ");
	    int mPort = scan.nextInt();
	    byte[] receiveData = new byte[1024];
        byte[] sendData = new byte[1024];
        InetAddress mAddr = InetAddress.getByName(mHost);
	    
	    try {
	    	serverSocket = new DatagramSocket(port);
	    	System.out.println("TCP Server waiting for client on port " + port);
	    }catch (IOException e) {
		    System.out.println("Could not listen on port" + port);
		    System.exit(0);
		}
	    DatagramPacket receive = null;
	    try {
	    	receive = new DatagramPacket(receiveData, receiveData.length);
		    serverSocket.receive(receive);
	    } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(0);
        }
	    
	    System.out.println("The client " + receive.getAddress() + ":" + serverSocket.getLocalPort() + " is now connected.");
	    while(true) {	
	    	//This is where the data communications happen. 
	    	DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
		    serverSocket.receive(receivePacket);
	        String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());
	        DatagramPacket mPacket = new DatagramPacket(sentence.getBytes(), sentence.getBytes().length, mAddr, mPort);
	        mSocket.send(mPacket);
	        System.out.println("RECEIVED: " + sentence);	
	        //If the client has been d/c, end the program.
	        if(sentence.equals("The client has been disconnected!")){
	        	System.out.println("Server has stopped.");
				serverSocket.close();
				break;
	        }

	        BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
	        System.out.println("SEND(Type Q or q to Quit)");
	        sentence = inFromUser.readLine();
	        if(sentence.equals("q") || sentence.equals("Q")) {
	        	System.out.println("Server has stopped.");
	        	String err = "The connection to the host has been disconnected!";
	        	sendData = err.getBytes();
	        	DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, receivePacket.getAddress(), receivePacket.getPort());
	        	serverSocket.send(sendPacket);
				serverSocket.close();
				inFromUser.close();
				break;
	        }
	        sendData = sentence.getBytes();
			DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, receivePacket.getAddress(), receivePacket.getPort());
	        serverSocket.send(sendPacket);
	    }
    }
}
